<template>
  <div class="reportFormPage">
    <div class="report-dom">
      <el-row>
        <el-col :span="5" :offset="19">
          <div class="pageNumBox">
            <span>第</span>
            <span class="placeholder-min text-center">&nbsp;{{ form.page }}&nbsp;</span>
            <span>页，共</span>
            <span class="placeholder-min text-center">&nbsp;{{ form.totalPage }}&nbsp;</span>
            <span>页</span>
          </div>
        </el-col>
      </el-row>
      <div class="topTwoBox">
        <div class="centerName">路基路面压实度试验检测报告(灌砂法)</div>
        <div class="codeText">
          <tr>
            BGLP01009F
          </tr>
        </div>
      </div>
      <div class="threeTopBox">
        <div class="left-border">
          <div class="box-title">检测单位名称：</div>
          <div class>{{ form.jiancdwmc }}</div>
        </div>
        <div class="right-border">
          <span>报告编号：</span>
          <span v-if="isPdf" class="placeholder">{{ form.baogbh }}</span>
          <el-input class="code-input" v-else type="text" v-model="form.baogbh" @focus="handleFocus('baogbh')" />
        </div>
      </div>
      <div class="table-border">
        <!-- .........................1111111111111111111111111111111......................................................... -->
        <table height="220px" class="top-table">
          <tbody>
            <tr>
              <td height="30" align="center" width="15%">
                <span style="width: 90px; display: inline-block">施工/委托单位</span>
              </td>
              <td width="35%" bgcolor="#ffffff">
                <span>{{ form.shigwtdw }}</span>
              </td>
              <td align="center" width="15%">
                <span style="width: 90px; display: inline-block">工程名称</span>
              </td>
              <td bgcolor="#ffffff" width="35%">
                <span>{{ form.gongcmc }}</span>
              </td>
            </tr>
            <tr>
              <td height="30" align="center" width="15%">
                <span style="width: 90px; display: inline-block">工程部位/用途</span>
              </td>
              <td colspan="3">
                <span>{{ form.gongcbwyt }}</span>
              </td>
            </tr>
            <tr>
              <td height="50" align="center">样品信息</td>
              <td class="specimen-border" colspan="3">
                <span>样品名称：</span>
                <span :class="{ placeholder: !form.yangpmc }">{{ form.yangpmc }}</span>
                <span>；样品编号：</span>
                <span :class="{ 'placeholder-long': !form.yangpbh }">{{ form.yangpbh }}</span>
                <span>；检测点数：</span>
                <span :class="{ placeholder: !form.yangpsl }">{{ form.yangpsl }}</span>
                <span>；样品状态：</span>
                <span :class="{ placeholder: !form.yangpzt }">{{ form.yangpzt }}</span>
              </td>
            </tr>
            <tr>
              <td height="30" align="center" width="15%">
                <span style="width: 90px; display: inline-block">检测依据</span>
              </td>
              <td width="35%">
                <span v-if="isPdf">{{ form.jiancyj }}</span>
                <el-input v-else type="textarea" v-model="form.jiancyj" :rows="2" class="mark" @dblclick.native="showBsJiancyjView" readonly />
              </td>
              <td align="center" width="15%">
                <span style="width: 90px; display: inline-block">判定依据</span>
              </td>
              <td width="35%">
                <span v-if="isPdf">{{ form.pandyj }}</span>
                <el-input v-else type="textarea" v-model="form.pandyj" :rows="2" @dblclick.native="showBsJudgeBaseView" @focus="handleFocus('pandyj')" class="mark" />
              </td>
            </tr>
            <tr class="bottom-border">
              <td height="30" align="center">
                <div>主要仪器设备</div>
                <div>名称及编号</div>
              </td>
              <td colspan="3">
                <span v-if="isPdf">{{ form.zhuyyqsbmcjbh }}</span>
                <el-input v-else type="textarea" :rows="2" v-model="form.zhuyyqsbmcjbh" class="height100 width100 mark" @dblclick.native="showBsInstrumentView" readonly></el-input>
              </td>
            </tr>
          </tbody>
        </table>
        <!-- .................................22222222222222222222222222222222............................................... -->
        <table height="400px" class="middle-table">
          <tbody>
            <tr height="25">
              <td width="35%">设计压实度(%)</td>
              <td width="15%"><el-input type="text" v-model="form.shejysd" disabled /></td>
              <td width="35%">结构层位</td>
              <td width="15%"><el-input type="text" v-model="form.jiegcw" disabled /></td>
            </tr>
            <tr height="25">
              <td>取样桩号、位置</td>
              <td>压实度(%)</td>
              <td>取样桩号、位置</td>
              <td>压实度(%)</td>
            </tr>
            <tr height="25" v-if="index % 2 == 0" v-for="(item, index) in form.detailList" :key="index">
              <td>
                <!-- {{ `${index / 2}_0` }} -->
                <el-input v-model="item.qvyzhwz" @keydown.native="handleKeyDown($event, index / 2, 0)" :ref="`${index / 2}_0`" />
              </td>
              <td><el-input :type="isPdf ? 'string' : 'number'" v-model="item.yasd" @keydown.native="handleKeyDown($event, index / 2, 1)" :ref="`${index / 2}_1`" /></td>
              <td><el-input v-model="form.detailList[index + 1].qvyzhwz" @keydown.native="handleKeyDown($event, index / 2, 2)" :ref="`${index / 2}_2`" /></td>
              <td><el-input :type="isPdf ? 'string' : 'number'" v-model="form.detailList[index + 1].yasd" @keydown.native="handleKeyDown($event, index / 2, 3)" :ref="`${index / 2}_3`" /></td>
            </tr>
          </tbody>
        </table>
        <table height="100px" class="middle-table">
          <tbody>
            <tr height="25">
              <td width="16%">压实度设计值(%)</td>
              <td width="11%"><el-input type="text" v-model="form.yasdsjz" disabled /></td>
              <td width="14%">压实度极值(%)</td>
              <td width="11%"><el-input type="text" v-model="form.yasdjz" @keydown.native="handleKeyDown($event, 16, 0)" :ref="`16_0`" /></td>
              <td width="13%">保证率(%)</td>
              <td width="11%"><el-input type="text" v-model="form.baozl" @keydown.native="handleKeyDown($event, 16, 1)" :ref="`16_1`" /></td>
              <td width="13%">平均值(%)</td>
              <td width="11%"><el-input type="text" v-model="form.pingjz" disabled /></td>
            </tr>
            <tr height="25">
              <td>代表值(%)</td>
              <td><el-input type="text" v-model="form.daibz" disabled /></td>
              <td>检测点数</td>
              <td><el-input type="text" v-model="form.jiancds" disabled /></td>
              <td>合格点数</td>
              <td><el-input type="text" v-model="form.hegds" disabled /></td>
              <td>合格率(%)</td>
              <td><el-input type="text" v-model="form.hegl" disabled /></td>
            </tr>
            <tr>
              <td colspan="8" height="60px">
                <span v-if="isPdf" class="textarea-span">{{ form.jiancjl }}</span>
                <el-input v-else type="textarea" :rows="2" v-model="form.jiancjl" @focus="handleFocus('jiancjl')" @dblclick.native="getJcjl()" />
              </td>
            </tr>
          </tbody>
        </table>
        <!-- .....................................................33333333333333333................................................... -->
        <table height="60px" class="bottom-table">
          <tr>
            <td>
              <span v-if="isPdf" class="textarea-span">{{ form.fujsm }}</span>
              <el-input v-else type="textarea" :rows="2" v-model="form.fujsm" @focus="handleFocus('fujsm')" />
            </td>
          </tr>
        </table>
      </div>
      <!-- .................................44444444444............................................. -->
      <table height="22px" class="transparent">
        <tbody>
          <tr>
            <td width="10%" align="right">检测：</td>
            <td width="15%" align="left"></td>
            <td width="10%" align="right">审核：</td>
            <td width="15%" align="left"></td>
            <td width="10%" align="right">批准：</td>
            <td align="left" width="15%"></td>
            <td width="10%">日期：</td>
            <td align="left" width="15%">
              <div style="display: inline-block; margin-left: 20px">年</div>
              <div style="display: inline-block; margin-left: 20px">月</div>
              <div style="display: inline-block ;margin-left: 20px">日</div>
            </td>
          </tr>
        </tbody>
      </table>
    </div>
    <bs-instrument-view ref="bsInstrumentViewRef" @select="selectInstrument"></bs-instrument-view>
    <bs-jiancyj-view ref="bsJiancyjViewRef" @select="selectJiancyj" :syType="syType"></bs-jiancyj-view>
    <bs-judge-base-view ref="bsJudgeBaseViewRef" @select="selectJudge" :syType="syType"></bs-judge-base-view>
  </div>
</template>
<script>
import { getLjlmNum } from "@/views/reportForms/utils/ljlmData";
import { handleKeyDown, handleFocus, emptyConvert, uniqueStr, calculateMeanvalue, calculateStandardDeviation } from "@/views/reportForms/utils";
import { getYsdgsfbgBsInfo, queryJcjl } from "@/api/report_ljlm/ysdgsfbgBsInfo";
import BsJudgeBaseView from "@/views/sycs/bsJudgeBase/BsJudgeBaseView.vue";
import BsInstrumentView from "@/views/dept/bsInstrument/BsInstrumentView.vue";
import BsJiancyjView from "@/views/sycs/bsJiancyj/BsJiancyjView.vue";
import BsJudgeBaseEditView from "@/views/sycs/bsJudgeBase/BsJudgeBaseEditView.vue";
import decimal from "@/utils/big-decimal";

import Bus from "@/utils/bus";
export default {
  name: "ysdgsfbg",
  components: {
    BsJudgeBaseView,
    BsInstrumentView,
    BsJiancyjView,
    BsJudgeBaseEditView
  },
  props: {
    excelId: {
      type: [String, Number],
      default: () => ""
    },
    syType: {
      type: [String, Number],
      default: () => ""
    },
    page: {
      type: Number,
      default: () => 0
    },
    isPdf: {
      type: Boolean,
      default: () => false
    },
    printEmpty: {
      type: Boolean,
      default: () => false
    }
  },
  data() {
    return {
      params: {},
      query: {},
      // 表单参数
      form: {
        detailList: [{}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}]
      },
      // 表单校验
      rules: {
        excelId: [{ required: true, message: "主键不能为空", trigger: "blur" }]
      }
    };
  },
  watch: {
    isPdf(newVal, oldVal) {
      this.form = { ...emptyConvert(this.form, newVal) };
    }
  },
  created() {
    this.getInfo();
  },
  mounted() {
    // 检测依据
    Bus.$on("setYsdgsfBgJiancyj", (data) => {
      const { jiancyj, jiancyjIds } = data;
      const str = jiancyj + (this.form.jiancyj ? "、" + this.form.jiancyj : "");
      const ids = jiancyjIds + (this.form.jiancyjIds ? "," + this.form.jiancyjIds : "");
      this.form.jiancyj = uniqueStr(str, "、");
      this.form.jiancyjIds = uniqueStr(ids, ",");
    });
    // 主要设备
    Bus.$on("setYsdgsfBgZhuyyqsbmcjbh", (data) => {
      const { zhuyyqsbmcjbh, shebIds } = data;
      const str = zhuyyqsbmcjbh + (this.form.zhuyyqsbmcjbh ? "、" + this.form.zhuyyqsbmcjbh : "");
      const ids = shebIds + (this.form.shebIds ? "," + this.form.shebIds : "");
      this.form.zhuyyqsbmcjbh = uniqueStr(str, "、");
      this.form.shebIds = uniqueStr(ids, ",");
    });

    // 获取数据;
    Bus.$on("setYsdgsfBgData", (data) => {
      const arr = ["qvyzhwz", "yasd"];
      data.forEach((item, index) => {
        arr.forEach((key) => {
          this.form.detailList[index][key] = item[key];
        });
      });
      this.$emit("getDetailListByJl", "ysdgsfbg");
    });
    // 获取顶部数据
    Bus.$on("setYsdgsfBgDataTop", (data) => {
      for (const key in data) {
        if (Object.prototype.hasOwnProperty.call(data, key)) {
          this.form[key] = data[key];
        }
      }
      this.$emit("getDetailListByJl", "ysdgsfbg");
    });
  },

  beforeDestroy() {
    // 或者你可以使用其他合适的生命周期钩子
    // 停止监听事件
    Bus.$off("setYsdgsfBgJiancyj");
    Bus.$off("setYsdgsfBgZhuyyqsbmcjbh");
    Bus.$off("setYsdgsfBgData" + this.page);
    Bus.$off("setYsdgsfBgDataTop");
  },

  methods: {
    // 获取详情
    getInfo() {
      getYsdgsfbgBsInfo(this.excelId || this.$defaultExcelId).then((response) => {
        this.form = response.data;
        if (!this.form.jiancjl) {
          this.form.jiancjl = "检测结论：";
        }
        if (!this.form.fujsm) {
          this.form.fujsm = "附加声明：";
        }
      });
    },

    // 显示检测依据列表
    showBsJiancyjView() {
      this.$refs.bsJiancyjViewRef.init(this.form.jiancyjIds);
    },
    // 检测依据选择
    selectJiancyj(info) {
      this.form.jiancyj = info.label;
      this.form.jiancyjIds = info.ids;
    },

    // 显示判定依据列表
    showBsJudgeBaseView() {
      this.$refs.bsJudgeBaseViewRef.init(this.form.pandyjIds);
    },
    // 判断依据选择
    selectJudge(info) {
       this.form.pandyjIds = info.ids;
      this.form.pandyj = info.label;
    },

    // 显示仪器列表
    showBsInstrumentView() {
      this.$refs.bsInstrumentViewRef.init(this.form.shebIds);
    },
    // 仪器选择
    selectInstrument(info) {
      this.form.zhuyyqsbmcjbh = info.label;
      this.form.shebIds = info.ids;
    },
    // 计算平均值
    calculateBg(detailList) {
      let pingjz = null; // 平均值(%)
      let daibz = null; // 代表值
      let jiancds = 0; // 检测点数
      let hegds = 0; // 合格点数
      let hegl = null; // 合格率(%)
      const shejysd = this.form.shejysd; // 设计值
      let arr = [];
      detailList.forEach((item) => {
        if (item.yasd) {
          arr.push(item.yasd);
          jiancds++;
          if (shejysd && Number(item.yasd) >= Number(shejysd)) {
            hegds++;
          }
        }
      });
      if (arr.length > 0) {
        pingjz = calculateMeanvalue(arr, 1);
        const biaozc = calculateStandardDeviation(arr);
        // 代表值=平均值 - (1.177（数量查询获取）)*标准差
        daibz = decimal.subtract(pingjz, decimal.multiply(getLjlmNum(jiancds), biaozc, 100), 1);
        hegl = decimal.multiply(decimal.divide(hegds, jiancds, 10), 100, 1);
      }

      this.form.pingjz = pingjz; // 平均值(%)
      this.form.daibz = daibz; // 代表值
      this.form.jiancds = jiancds; // 检测点数
      this.form.hegds = hegds; // 合格点数
      this.form.hegl = hegl; // 合格率(%)
    },
    // 获取检测结论
    getJcjl() {
      const form = JSON.parse(JSON.stringify(this.form));
      queryJcjl(emptyConvert(form)).then((res) => {
        const info = res.data;
        this.form.jiancjl = info.jiancjl;
      });
    },

    submitForm() {
      return this.form;
    },
    handleFocus(event) {
      handleFocus(event, this.page, this.form[event]);
    },
    handleKeyDown(event, rowIndex, colIndex) {
      const maxArr = [3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1];
      const ref = handleKeyDown(event, rowIndex, colIndex, maxArr);
      if (this.$refs[ref]?.length) {
        this.$refs[ref][0].focus();
      } else if (this.$refs[ref]) {
        this.$refs[ref].focus();
      }
    }
  }
};
</script>

<style scoped></style>
